<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="120px">


      <el-form-item :label="$t('orderNumber') " prop="orderSn">
        <el-input
          v-model="queryParams.orderSn"
          :placeholder="$t('pleaseEnterTheOrderNumber')"
          clearable
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item :label="$t('userName')" prop="userName">
        <el-input
          v-model="queryParams.userName"
          :placeholder="$t('enterOneUserName')"
          clearable
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>


      <el-form-item :label="$t('phoneNumber')" prop="userPhone">
        <el-input
          v-model="queryParams.userPhone"
          :placeholder="$t('pleaseEnterYourMobileNumber')"
          clearable
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item :label="$t('dueTime')">
        <el-date-picker
          v-model="dateRange"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          :start-placeholder="$t('startTime')"
          :end-placeholder="$t('endTime')"
        ></el-date-picker>
      </el-form-item>


      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">{{$t('search')}}</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">{{ $t('reset') }}</el-button>
      </el-form-item>
    </el-form>

    <el-table v-loading="loading" :data="applyList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="50" align="center"/>
      <el-table-column :label="$t('platform')" align="center" prop="companyId"/>
      <el-table-column :label="$t('orderNumber')" align="center" prop="orderSn"/>
      <el-table-column :label="$t('userName')" align="center" prop="userName"/>
      <el-table-column :label="$t('phoneNumber')" align="center" prop="userPhone"/>

      <el-table-column :label="$t('loanAmount')" align="center" prop="applyMoney">
        <template slot-scope="scope">
          {{ scope.row.realApplyMoney - scope.row.manageMoney - scope.row.riskMoney - scope.row.applyServiceCharge }}
        </template>
      </el-table-column>
      <el-table-column :label="$t('amountPayable')" align="center" prop="applyMoney">
        <template slot-scope="scope">
          {{ scope.row.realApplyMoney + scope.row.overMoney }}
        </template>
      </el-table-column>
      <el-table-column :label="$t('serviceFee')" align="center" prop="applyServiceCharge" />
      <el-table-column :label="$t('lateFee')" align="center" prop="overMoney"/>
      <el-table-column :label="$t('amountRepaid')" align="center" prop="alreadyRepayMoney" />
      <el-table-column :label="$t('remainingToBeRepaid')" align="center" prop="applyMoney">
        <template slot-scope="scope">
          {{ scope.row.realApplyMoney + scope.row.overMoney - scope.row.alreadyRepayMoney }}
        </template>
      </el-table-column>
      <el-table-column :label="$t('payTime')" align="center" prop="applyTime" width="160">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.applyTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('dueTime')" align="center" prop="repaymentTime" width="160">
        <template slot-scope="scope">
          <span>{{ scope.row.repaymentTime }}</span>
        </template>
      </el-table-column>
      <el-table-column
        :label="$t('operate')"
        align="center"
        width="160"
        fixed="right"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-search"
          >
            <router-link :to="'/app/user-info/index/' + scope.row.id + '/' + scope.row.userId" class="link-type">
              <span>{{$t('see')}}</span>
            </router-link>
          </el-button>
          <!--<el-button-->
            <!--size="mini"-->
            <!--type="text"-->
            <!--icon="el-icon-plus"-->
            <!--@click="handleAdd(scope.row)"-->
          <!--&gt;添加-->
          <!--</el-button>-->
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <el-dialog title="催收记录" :visible.sync="open1" width="600px">
      <el-form ref="form1" :model="form1" :rules="rules1" label-width="120px">
        <el-form-item label="备注" prop="remark">
          <el-select v-model="form1.remark" placeholder="请选择" clearable>
            <el-option
              v-for="dict in dict.type.over_des"
              :key="dict.label"
              :label="dict.label"
              :value="dict.label"
            />
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitRemarkHandler">确 定</el-button>
        <el-button @click="open1 = false">取 消</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
  import {listOverRepaymentApply, handlePayment, addOverLogForm} from "@/api/app/apply";
  import {list} from "@/api/company";
  import {getUserVa} from "@/api/app/user"

  export default {
    name: "Over",
    dicts: ['over_des'],
    data() {
      return {
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 订单表格数据
        applyList: null,
        // 公司数据
        companyList: null,
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        open1: false,
        // 日期范围
        dateRange: [],
        // 表单参数
        form: {},
        form1: {},
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          orderSn: undefined,
          userPhone: undefined,
          uesrName: undefined
        },
        // 表单校验
        rules: {
          paymentMoney: [
            {required: true, message: "还款金额不能为空", trigger: "blur"},
          ]
        },
        rules1: {
          remark: [
            {required: true, message: "备注不能为空", trigger: "blur"},
          ]
        }
      };
    },
    watch: {},
    created() {
      this.getList();
    },
    methods: {
      /** 查询列表 */
      getList() {
        this.loading = true;
        if (this.$store.state.user.companyId) {
          this.queryParams.companyId = this.$store.state.user.companyId
        }
        listOverRepaymentApply(this.addDateRange(this.queryParams, this.dateRange)).then(response => {
            this.applyList = response.rows;
            this.total = response.total;
            this.loading = false;
          }
        );
      },

      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          id: undefined,
          realApplyMoney: 0,
          manageMoney: 0,
          riskMoney: 0,
          applyServiceCharge: 0,
          applyCycle: 0,
          realPayMoney: 0,
          interestMoney: 0,
          overDay: 0,
          overMoney: 0,
          paymentMoney: 0
        };
        this.form1 = {
          id: undefined,
          remark: undefined
        };
        this.resetForm("form");
        this.resetForm("form1");
      },

      /** 重置按钮操作 */
      resetQuery() {
        this.dateRange = [];
        this.resetForm("queryForm");
        this.handleQuery();
      },

      openUserVa(row) {
        //获取虚拟卡信息
        getUserVa({applyId: row.id}).then(response => {
            var str = '<h3>'+this.$t('userName')+'：' + response.data.accountName + '</h3>';
            if (!isNaN(response.data.bank)) {
              str += '<h3>'+this.$t('idNumber')+'：' + response.data.accountNo + '</h3>';
            } else {
              str += '<h3>'+this.$t('idNumber')+'：' + response.data.bank + '</h3>';
            }
            str += '<h3>'+this.$t('bank')+'：PVCOMBANK</h3>';
            this.$alert(str, this.$t('virtualCardInformation'), {
              dangerouslyUseHTMLString: true
            });
          }
        );

      },

      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },

      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.id);
        this.single = selection.length != 1;
        this.multiple = !selection.length;
      },

      /** 新增按钮操作 */
      handleAdd(row) {
        this.reset();
        this.form1.id = row.id;
        this.open1 = true;
      },
      /** 修改按钮操作 */
      handlePay(row) {
        this.reset();
        // const id = row.id;
        // getApply(id).then(response => {
        this.form = row;
        this.form.realPayMoney = this.form.realApplyMoney - this.form.manageMoney - this.form.riskMoney - this.form.applyServiceCharge;
        this.open = true;
        this.title = "还款";
        // });
      },

      submitHandlerPayment() {
        this.$refs["form"].validate(valid => {
          if (valid) {
            let that = this.form;
            this.$modal.confirm('是否确认对编号为"' + this.form.orderSn + '"的订单还款？').then(function () {
              return handlePayment(that.id, '0', that.paymentMoney);
            }).then(() => {
              this.getList();
              this.open = false;
              this.$modal.msgSuccess("还款成功");
            }).catch((e) => {
            });
          }
        })
      },

      submitRemarkHandler() {
        this.$refs["form1"].validate(valid => {
          if (valid) {
            addOverLogForm(this.form1).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open1 = false;
              this.getList();
            });
          }
        })
      },

      /** 删除按钮操作 */
      handleDelete(row) {
        // const ids = row.id || this.ids;
        // this.$modal.confirm('是否确认删除编号为"' + ids + '"的数据项？').then(function () {
        //   return delIndexApply(ids);
        // }).then(() => {
        //   this.getList();
        //   this.$modal.msgSuccess("删除成功");
        // }).catch(() => {
        // });
      },

      // handleMoney(){
      //   this.form.realMoney = this.form.applyMoney - this.form.manageMoney - this.form.examineMoney - this.form.serviceMoney;
      // }
    }
  };
</script>
